%% Aim
% Identify gaze data from eye data

%% Setup
tic;
clc;
clear;
root_path = pwd;
addpath(genpath(root_path));

nrole=2;
nblock=2;
ntrial=100;
nprice=10;
nlottery=10;

role_label={'Buyer','Seller'};
  
%% Load data
sub_data=readtable([root_path,'\data\subject\subject.csv']);
nsub=height(sub_data);
eye_data=readtable([root_path,'\data\eye\eye.csv']);

%% Identify gaze data for each subject

for isub = 1:nsub
    for irole = 1:nrole

        
        raw_data = eye_data(eye_data.Subject==isub & eye_data.Role==irole,:);

        gaze_data=raw_data;

        % remove invalid values: -1, 0, NaN
        gaze_data(gaze_data.GazeX<=0 | gaze_data.GazeY<=0 | isnan(gaze_data.GazeX) | isnan(gaze_data.GazeY),:) = [];

        %% categorize x and y
        locations = [];
        for i = 1:height(gaze_data)
            x = gaze_data.GazeX(i);
            y = gaze_data.GazeY(i);
            if y >= 275 && y <= 775
                % if fix is in the left square
                if x >= 160 && x <= 660
                    locations = [locations; -1];
                % if fix is in the center
                elseif x > 660 && x < 1020
                    locations = [locations; 0];
                % if fix is in the right square
                elseif x >= 1020 && x <= 1520
                    locations = [locations; 1];
                else
                    locations = [locations; 9];
                end
            else
                locations = [locations; 9];
            end
        end
        gaze_data.Location = locations;
    
       %% find indices of change (either stimulus change or fixation change), take only rows where a change occurred
        prevLocation = gaze_data.Location(1);
        prevStim = gaze_data.StimulusName(1);
        changes = [];
        changes(1) = 1;
        for i = 1:height(gaze_data)
            if gaze_data.Location(i) ~= prevLocation || gaze_data.StimulusName(i) ~= prevStim
                changes = [changes;i];
                prevLocation = gaze_data.Location(i);
                prevStim = gaze_data.StimulusName(i);
            end
        end
    
        gaze_data = gaze_data(changes,:);
    
        %% get gaze lengths, remove gaze lengths only after a threshold
        times = [];
        for i = 1:height(gaze_data) - 1
            times(i) = gaze_data.TimeSignal(i + 1) - gaze_data.TimeSignal(i);
        end

        times(i + 1) = raw_data.TimeSignal(length(raw_data.TimeSignal)) - gaze_data.TimeSignal(i + 1);

        gaze_data.GazeTime = times.';

        % remove gaze lengths below a threshold
        threshold = 50;
        toRemove = [];
        for i = 1:length(gaze_data.GazeTime)
            if gaze_data.GazeTime(i) < threshold
                toRemove = [toRemove, i];
            end
        end

        gaze_data(toRemove,:) = [];

       %% collapse consecutive gazes
    
        gazeTimes = [];
        toRemove = [];

        prevStim = gaze_data.StimulusName(1);
        prevLocation = gaze_data.Location(1);
        gaze = gaze_data.GazeTime(1);

        for i = 2:height(gaze_data)
            if (gaze_data.Location(i) == prevLocation) && (gaze_data.StimulusName(i) == prevStim)
                toRemove = [toRemove;i];
                gaze = gaze + gaze_data.GazeTime(i);
            else
                gazeTimes = [gazeTimes; gaze];
                prevLocation = gaze_data.Location(i);
                prevStim = gaze_data.StimulusName(i);
                gaze = gaze_data.GazeTime(i);
            end
        end

        % collapse the last gaze
        gazeTimes = [gazeTimes;gaze];

        gaze_data(toRemove,:) = [];
        gaze_data.GazeTime = gazeTimes;

       %% save gaze data
        writetable(gaze_data, [root_path, '\data\gaze\s', int2str(isub), '_r', int2str(irole), '_gaze.csv']);

    end
end

%% Generate the concatenated .csv file

gaze_data=[];
for isub = 1:nsub
    for irole = 1:nrole
        gaze_data = [gaze_data; readtable([root_path,'\data\gaze\s', int2str(isub), '_r', int2str(irole), '_gaze.csv'])];
    end
end
writetable(gaze_data, [root_path, '\data\gaze\gaze.csv']);

%%
toc;
